# --*-- coding: utf-8 --*--
"""测试常用的基础工具类"""
import json
import os
import random
import shutil
import ssl
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE

load_wait_short = 2
load_wait_middle = 4
load_wait_long = 6


class ChromeControl(object):
    """selenium连接控制以调试模式启动的chrome浏览器"""

    def __init__(self, port, chrome_driver=r"D:\seleniumChrome\chromedriver.exe"):
        """
        :param port: 本地CMD窗口以调试模式启动的chrome的端口号
          chrome.exe --remote-debugging-port=9233 --user-data-dir="C:\Users\Administrator\AppData\Local\Google\Chrome\Application"
        :param chrome_driver: windows本地驱动存放的绝对路径
        """
        chrome_options = Options()
        chrome_options.add_experimental_option("debuggerAddress", f"127.0.0.1:{port}")
        chrome_options.add_argument("--disable-extensions")
        """大部分的性能优化手段都在options中进行设置"""
        # 配置headless模型
        chrome_options.add_argument("--headless")  # 无头模式
        chrome_options.add_argument("--disable-gpu")  # 禁用cpu渲染
        chrome_options.add_argument("--disable-software-rasterizer")
        # 配置不加载图片
        chrome_options.add_argument("blink-settings=imagesEnabled=false")
        # 禁用插件加载
        chrome_options.add_argument("--disable-extensions")
        self.browser = webdriver.Chrome(chrome_driver, options=chrome_options)
        self.browser.implicitly_wait(10)

    def clickNextPage(self):
        """使用示例"""
        ele = self.browser.find_elements(By.XPATH, "//li[@class=' ant-pagination-next']/a")
        if len(ele):
            self.browser.execute_script("arguments[0].click();", ele[0])
        else:
            return False


if __name__ == '__main__':
    TestUtils = TestUtils()
